if (window.plus) {
	ready();
} else {
	document.addEventListener('plusready', ready, false);
}

$(function() {
	if (AGENT_CLIENT.WEB) {
		ready();
	}
})

function ready() {
	var user; //用户信息
	var query = {
		"index": 1,
		"tip": getUrlParam("type")
	}; //当前页
	var fromurl = getUrlParam("fromurl"); //来源
	var html = []; //html字符串
	var myscroll; //滚动
	var thispage; //当前页
	var pullUp;
	var pullDown;
	if (!query.tip || undefined == query.tip) {
		query.tip = "goods";
	}
	var attentionPage = function() {
		this.init();
	};
	attentionPage.prototype = {
		init: function() {
			this.addScroll();
			this.loadData();
			this.bindEvent();
		},
		addScroll: function() {
			pullUp = $("#pullUp");
			pullDown = $("#pullDown");
			myscroll = new IScroll('#wrapper', {
				probeType: 2,
				scrollbars: true,
				mouseWheel: false,
				fadeScrollbars: true,
				bounce: true,
				interactiveScrollbars: false,
				shrinkScrollbars: 'scale',
				click: true,
				keyBindings: true,
				momentum: true
			});
			myscroll.on("scrollEnd", function() {
				if ((this.y < 0) && (this.y == this.maxScrollY)) {
					console.log("滚动到底部");
					pullUp.show();
					setTimeout(function() {
						query.index++;
						thispage.loadAttentions("add");
					}, 500);
				} else if ((this.y == 0) && (this.y != this.maxScrollY)) {
					console.log("滚动到顶部");
					pullDown.show();
					setTimeout(function() {
						query.index = 1;
						thispage.loadAttentions("renovate");
					}, 500);
				}
			});
		},
		//加载
		loadData: function() {
			user = getLocalStorage(STORAGE_MANAGER.user);
			if (!user) {
				redirect("../account/login.html");
				return false;
			}
			this.movetab();
			pullDown.show();
			this.loadAttentions('renovate');
		},
		loadAttentions: function(type) {
			var requestUrl = CONFIG_MANAGER.RequstUrl;
			var requestType = "GET";
			var requestData = {};
			switch (query.tip) {
				case "goods":
					requestUrl += "v1/member/favorite/goods/";
					requestUrl += "{0}-{1}-{2}".format(user.id, query.index, CONFIG_MANAGER.pageSize);
					break;
				case "shop":
					requestUrl += "v1/member/favorite/shop/";
					requestUrl += "{0}-{1}-{2}".format(user.id, query.index, CONFIG_MANAGER.pageSize);
					break;
				case "trace":
					requestUrl += "v1/history/lists";
					requestType = "POST";
					requestData = {
						index: query.index,
						size: CONFIG_MANAGER.pageSize
					};					
					break;
				default:
					break;
			}

			$.ajax({
				type: requestType,
				url: requestUrl,
				async: true,
				data: requestData,
				headers: {
					"token": user.token
				}
			}).done(function(data) {
				thispage.loadList(JSON.parse(data), query.tip, type);
			}).always(function(data) {});
		},
		//加载列表
		loadList: function(data, tip, type) {
			var src, tips = {},
				id, attention = data.data,
				html = [];
			if (0 == attention.length && 'renovate' == type) {
				$("#attentionList").html('<div class="zhanwushuju"><img src="../../images/zanwushuju.png"><br>暂无数据</div>');
			} else {
				$.each(attention, function(i, item) {
					src = "";
					title = "";
					tips.TipType = tip;
					if (item.goods) {						
						tips.id = item.goods.sku_id;
						src = (item.goods.logo) ? CONFIG_MANAGER.imgeUrl + item.goods.logo : null;
						title = item.goods.title;
					} else if (item.shop) {
						tips.id = item.shop.id;
						src = (item.shop.logo) ? CONFIG_MANAGER.imgeUrl + item.shop.logo : null;
						title = item.shop;
					}
					if (!src) {
						src = "../../images/dlogo.png";
					}
					html.push('<div class="mbbottom with100 overflow whitebg">');
					//if ("trace" != tip) {
					html.push('<p name="delAttention" tip=' + JSON.stringify(tips) + ' class="qxguanzhu">-</p>');
					//}
					html.push('<dl class="cpxqxztop cpxqxztop1 mtop0">');
					html.push('<dt name="favLink" tip=' + JSON.stringify(tips) + '><img src="' + src + '"></dt>');
					html.push('<dd name="favLink" tip=' + JSON.stringify(tips) + ' class="wdgzwz qhuise">' + title + '</dd>');
					html.push('<dd name="favLink" tip=' + JSON.stringify(tips) + ' class="wlxxlc wlxxlc5"><span><b>' + toDateString(item.create_time, "yyyy-MM-dd hh:mm") + '</b></span></dd>');
					html.push('</dl>');
					html.push('</div>');
				});
				if ("add" == type) {
					$(html.join('')).appendTo($("#attentionList"));
				} else {
					$("#attentionList").html(html.join(''));
				}
			}
			setTimeout(function() {
				myscroll.refresh();
				pullUp.hide();
				pullDown.hide();
			}, 100);
		},
		//页面事件
		bindEvent: function() {
			//点击回退
			$("#back").on("click", function() {
				backToPreviousPage(fromurl, true);
			});

			//点击“关注的宝贝”，“关注的店铺”，“我的足迹”
			$("li[name='mattention']").on("click", function() {
				query.tip = $(this).attr("tip");
				query.index = 1;
				thispage.movetab();
				$("#attentionList").html("");
				$(".zhanwushuju").remove();
				pullDown.show();
				console.log(query.tip);

				thispage.loadAttentions("renovate");
			});
			//点击跳转
			var t1 = null;
			$("#attentionList").on("click", "dd[name='favLink']", function() {
				if (AGENT_CLIENT.APP) {
					if (t1 == null) {
						t1 = new Date().getTime();
					} else {
						var t2 = new Date().getTime();
						if (t2 - t1 < 500) {
							t1 = t2;
							return;
						} else {
							t1 = t2;
						}
					}
				}
				var tips = JSON.parse($(this).attr("tip"));
				console.log(tips);
				if ("goods" == tips.TipType) {
					redirect("../goods/goodsdetail.html?id=" + tips.id);
				} else {
					redirect("../shop/shop.html?id=" + tips.id);
				}
			});
			//点击取消关注
			var t1 = null;
			$("#attentionList").on("click", "p[name='delAttention']", function(event) {
				if (AGENT_CLIENT.APP) {
					if (t1 == null) {
						t1 = new Date().getTime();
					} else {
						var t2 = new Date().getTime();
						if (t2 - t1 < 500) {
							t1 = t2;
							return;
						} else {
							t1 = t2;
						}
					}
				}
				var attention = $(this);
				var tips = JSON.parse(attention.attr("tip"));
				var requestUrl = CONFIG_MANAGER.RequstUrl + "v1/member/favorite/" + tips.TipType + "/" + user.id + "/" + tips.id;
				if("trace" == tips.TipType){
					requestUrl = CONFIG_MANAGER.RequstUrl + "v1/history/"+ getSessionID() +"/"+ tips.id + "/" + user.id;
				}
				$.confirm("删除记录?", function(e) {
					//console.log(requestUrl);
					if (0 == e || 0 == e.index) {
						$.ajax({
							type: "DELETE",
							url: requestUrl,
							async: true,
							headers: {
								"token": user.token
							},
							beforeSend: function() {
								$.showWaiting();
							}
						}).done(function(data) {
							if (data) {
								$.toast('删除成功！');
								attention.parent().remove();
								if (0 == $("#attentionList>div").length) {
									$("#attentionList").html('<div class="zhanwushuju"><img src="../../images/zanwushuju.png"><br>暂无数据</div>');
								}
							}
						}).fail(function(data) {
							$.toast(data.responseJSON.Message);
						}).always(function() {
							$.closeWaiting();
						});
					}
				});
				event.preventDefault();
			});
		},
		movetab: function() {
			var tip = $("li[tip=" + query.tip + "]");
			var value = "translateX(" + tip.offset().left + "px)";
			$("#bottowtab").css({
				"margin-left": "0px",
				"transform": value,
				"-moz-transform": value,
				"-webkit-transform": value,
				"-o-transform": value,
				"-ms-transform": value
			});
		}

	};
	thispage = new attentionPage();
}